﻿using Qins.PluginInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Application = System.Windows.Application;

namespace Qins.Plugins.NotifyIcon
{
    /// <summary>
    /// PluginManager.xaml 的交互逻辑
    /// </summary>
    public partial class PluginManager : Window
    {
        List<PluginInfo> lpi;
        QinNotifyIcon qni;
        public PluginManager()
        {
            InitializeComponent();
        }

        public PluginManager(List<PluginInfo> _lpi)
        {
            lpi= _lpi;
            InitializeComponent();
        }
        public PluginManager(QinNotifyIcon _qni,List<PluginInfo> _lpi)
        {
            qni= _qni;
            lpi = _lpi;
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach(PluginInfo info in lpi)
            {
                if (info.Name == "QinNotifyIcon")
                { lpi.Remove(info); break; }
            }
            PluginsData.ItemsSource = lpi;
        }

        private void PluginsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedRow = PluginsData.SelectedItem as PluginInfo;
            if (selectedRow != null)
            {
                tbKey.Text = selectedRow.Key;
                tbDescription.Text = selectedRow.Description;
                tbInDescription.Text = selectedRow.InDescription;
                tbOutDescription.Text = selectedRow.OutDescription;
            }
        }

        private void btnrun_Click(object sender, RoutedEventArgs e)
        {
            MessageContent mc = new MessageContent();
            mc.messageType = MessageConentTypeEnum.Run;
            mc.content = tbInDescription.Text;
            var reobj = qni.CallPluginRun(tbKey.Text, mc);
            tbOutDescription.Text = reobj?.ToString();

            //foreach (var ss in Application.Current.Windows.OfType<Window>())
            //{
            //    if (ss.Name == "QinNotifyIcon")
            //    {
            //        QinNotifyIcon qni = (QinNotifyIcon)ss;
            //        MessageContent mc = new MessageContent();
            //        mc.messageType = "Run";
            //        mc.content = tbInDescription.Text;
            //        var reobj = qni.CallPluginRun(tbKey.Text, mc);
            //        tbOutDescription.Text = reobj?.ToString();
            //        break;
            //    }
            //}
        }
    }
}
